package com.jf.apply_car.controller;

import com.google.code.kaptcha.Constants;
import com.jf.apply_car.bean.Userinfo;
import com.jf.apply_car.service.UserinfoService;
import com.jf.apply_car.util.MyConstants;
import com.jf.apply_car.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/login")
    @ResponseBody
    public ResultJson login(Userinfo userinfo, String code, HttpSession session) {
        /*获取验证码文本内容*/
        String kaptchaCode = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString();
        /*检查验证码*/
        if (!kaptchaCode.equalsIgnoreCase(code)) {
            return ResultJson.error("验证码错误！");
        }
        /*执行登录*/
        userinfo = userinfoService.login(userinfo);

        /*登录信息存入session*/
        session.setAttribute(MyConstants.LOGIN_SESSION_KEY, userinfo);
        return ResultJson.success();
    }
}
